﻿using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Text.RegularExpressions;

namespace HuoTu.Common
{
    /// <summary>
    /// 字符串操作类
    /// </summary>
    public class SysString
    {
        #region 虚拟路径转换
        /// <summary>
        /// 虚拟路径转换
        /// </summary>
        /// <param name="pathName">文件路径</param>
        /// <returns></returns>
        public static string ChangePath(string pathName)
        {
            string path = null;
            try
            {
                path = HttpContext.Current.Server.MapPath(pathName);
            }
            catch
            {
                path = pathName;
            }
            return path;
        }
        #endregion

        #region 客户端验证 IP
        /// <summary>  
        /// 取得客户端真实IP。如有代理则取第一个非内网地址  
        /// </summary>  
        public static string GetClientIP
        {
            get
            {
                string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                if (result != null && result != String.Empty)
                {
                    //可能有代理  
                    if (result.IndexOf(".") == -1) //非IPv4格式  
                        result = string.Empty;
                    else
                    {
                        if (result.IndexOf(",") != -1)
                        {
                            //有“,”，估计多个代理。取第一个不是内网的IP。  
                            result = result.Replace(" ", "").Replace("'", "");
                            string[] temparyip = result.Split(",;".ToCharArray());
                            for (int i = 0; i < temparyip.Length; i++)
                            {
                                if (IsIPAddress(temparyip[i])
                                && temparyip[i].Substring(0, 3) != "10."
                                && temparyip[i].Substring(0, 7) != "192.168"
                                && temparyip[i].Substring(0, 7) != "172.16.")
                                {
                                    return temparyip[i]; //找到不是内网的地址  
                                }
                            }
                        }
                        else if (IsIPAddress(result)) //代理即是IP格式  
                            return result;
                        else
                            result = string.Empty; //代理中的内容 非IP，取IP  
                    }

                }

                if (null == result || result == String.Empty)
                    result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                if (result == null || result == String.Empty)
                    result = HttpContext.Current.Request.UserHostAddress;
                return result;
            }
        }

        /// <summary>
        /// 检测是否本站提交
        /// </summary>
        /// <returns></returns>
        public static bool IsSelfRefer()
        {
            string sHttp_Referer, sServer_Name;
            sHttp_Referer = HttpContext.Current.Request.ServerVariables["HTTP_REFERER"];
            sServer_Name = HttpContext.Current.Request.Url.Host.ToLower();
            if (sHttp_Referer == null || sServer_Name == null)
                return false;
            if (sHttp_Referer.Substring(7, sServer_Name.Length) == sServer_Name)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 获取当前网站的网站网址
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentWebName()
        {
            return HttpContext.Current.Request.Url.Host.ToLower();
        }

        private static bool IsIPAddress(string str1)
        {
            if (str1 == null || str1 == string.Empty || str1.Length < 7 || str1.Length > 15) return false;
            string regformat = @"^\d{1,3}[\.]\d{1,3}[\.]\d{1,3}[\.]\d{1,3}$";
            Regex regex = new Regex(regformat, RegexOptions.IgnoreCase);
            return regex.IsMatch(str1);
        }

        /// <summary>
        /// 是否包含指定的IP
        /// </summary>
        /// <param name="sourceIp">源IP[以,隔开]</param>
        /// <param name="findIp">要查找的IP</param>
        /// <returns></returns>
        public static bool ContainIp(string sourceIp, string findIp)
        {
            bool isExistIp = false;
            string[] ip = sourceIp.Split(',');
            for (int i = 0; i < ip.Length; i++)
            {
                if (ip[i].IndexOf("-") < 0)
                {
                    isExistIp = false;
                    break;
                }
                string[] ipR = ip[i].Split('-');
                if (!SysRegex.IsValidIp(ipR[0]) || !SysRegex.IsValidIp(ipR[1]))
                {
                    isExistIp = false;
                    break;
                }
                if (IP2INT(ipR[0]) > IP2INT(ipR[1]))
                {
                    isExistIp = false;
                    break;
                }

                if (IP2INT(findIp) >= IP2INT(ipR[0]) && IP2INT(findIp) <= IP2INT(ipR[1]))
                {
                    isExistIp = true;
                    break;
                }
            }
            return isExistIp;
        }

        /// <summary>
        /// IP转成整数
        /// </summary>
        /// <param name="_ip"></param>
        /// <returns></returns>
        private static long IP2INT(string _ip)
        {
            string[] t = _ip.Split('.');
            return Convert.ToInt64(t[0]) * 256 * 256 * 256
                + Convert.ToInt64(t[1]) * 256 * 256
                + Convert.ToInt64(t[2]) * 256
                + Convert.ToInt64(t[3])
                - 1;
        }

        /// <summary>
        /// 判断是否包含Http头的地址
        /// </summary>
        /// <param name="Object">要操作的对象</param>
        /// <returns>bool</returns>
        public static bool IsHttpUrl(string Object)
        {
            if (SysBase.IsNull(Object)) return false;
            if (SysBase.Left(Object, 7).ToLower() == "http://") return true;
            return false;
        }
        #endregion

        #region SQL字符串处理
        /// <summary>
        /// 过滤文本中的sql关键字
        /// </summary>
        /// <param name="text">要处理的文本</param>
        public static string SqlFilter(string text)
        {
            text = text.ToLower();
            if (text.IndexOf("=") > -1) { text = text.Replace("=", ""); }
            if (text.IndexOf("*") > -1) { text = text.Replace("*", ""); }
            if (text.IndexOf('\'') > -1) { text = text.Replace("\'", "''"); }
            if (text.IndexOf("-") > -1) { text = text.Replace("-", ""); }
            if (text.IndexOf(" exec ") > -1) { text = text.Replace("exec", ""); }
            if (text.IndexOf(" execute ") > -1) { text = text.Replace("execute", ""); }
            if (text.IndexOf(" select ") > -1) { text = text.Replace("select", ""); }
            if (text.IndexOf(" insert ") > -1) { text = text.Replace("insert", ""); }
            if (text.IndexOf(" delete ") > -1) { text = text.Replace("delete", ""); }
            if (text.IndexOf(" update ") > -1) { text = text.Replace("update", ""); }
            if (text.IndexOf(" create ") > -1) { text = text.Replace("create", ""); }
            if (text.IndexOf(" alter ") > -1) { text = text.Replace("alter", ""); }
            if (text.IndexOf(" add ") > -1) { text = text.Replace("add", ""); }
            if (text.IndexOf(" drop ") > -1) { text = text.Replace("drop", ""); }
            if (text.IndexOf(" count ") > -1) { text = text.Replace("count", ""); }
            if (text.IndexOf(" chr ") > -1) { text = text.Replace("chr", ""); }
            if (text.IndexOf(" mid ") > -1) { text = text.Replace("mid", ""); }
            if (text.IndexOf(" master ") > -1) { text = text.Replace("master", ""); }
            if (text.IndexOf(" and ") > -1) { text = text.Replace("and", ""); }
            if (text.IndexOf(" or ") > -1) { text = text.Replace("or", ""); }
            if (text.IndexOf(" truncate ") > -1) { text = text.Replace("truncate", ""); }
            if (text.IndexOf(" set ") > -1) { text = text.Replace("set", ""); }
            if (text.IndexOf(" char ") > -1) { text = text.Replace("char", ""); }
            if (text.IndexOf(" declare ") > -1) { text = text.Replace("declare", ""); }
            if (text.IndexOf(" join ") > -1) { text = text.Replace("join", ""); }
            if (text.IndexOf(" dbcc ") > -1) { text = text.Replace("dbcc", ""); }
            if (text.IndexOf(" use ") > -1) { text = text.Replace("use", ""); }
            if (text.IndexOf(" open ") > -1) { text = text.Replace("open", ""); }
            if (text.IndexOf(" proc ") > -1) { text = text.Replace("proc", ""); }
            if (text.IndexOf(" procedure ") > -1) { text = text.Replace("procedure", ""); }

            return text;
        }
        /// <summary>
        /// 判断文本中是否存在sql关键字
        /// </summary>
        /// <param name="text">要处理的文本</param>
        public static bool ExistSql(string text)
        {
            string filterWord = "=|*|'|-|exec|execute|select|insert|delete|update|create|alter|add|drop|count|chr|mid|master|and|or|truncate|set|char|declare|join|dbcc|use|open|proc|procedure";
            if (text == null)
            {
                return false;
            }
            foreach (string _str in filterWord.Split('|'))
            {
                if ((text.ToLower().IndexOf(_str + " ") > -1) || (text.ToLower().IndexOf(" " + _str) > -1))
                {
                    return true;
                }
            }
            return false;
        }
        #endregion

        #region 随机字符串
        /// <summary>
        /// 自动随机串(0-9 A-Z)
        /// </summary>
        /// <param name="xLen">长度</param>
        /// <returns></returns>
        public static string RandomNo(int xLen)
        {
            long tNum = DateTime.Now.Ticks;
            string tStr = tNum.ToString("X16");
            if (xLen < tStr.Length) { tStr = tStr.Substring(0, xLen); }
            else { tStr += RndID("KEY", xLen - tStr.Length); }
            return tStr;
        }

        private static string RndID(string xType, int xLen)
        {
            string rChar;
            int rMax, i;
            string orgNum = "0123456789";     //  10   xType = 0,A,KEY
            string orgCap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //  26   .  -  _  $  |  !  #  [  ]  
            string orgKey = "ABCDEFGHJKLMNPQRSTUVWXY";      //  23   I  O  Z
            string oStr = "";
            string tStr = "";
            switch (xType)
            {
                case "0": rMax = 10; tStr = orgNum; break;
                case "A": rMax = 26; tStr = orgCap; break;
                default: rMax = 33; tStr = orgNum + orgKey; break;
            }
            System.Random ran = new Random(System.DateTime.Now.Second + (int)System.DateTime.Now.Ticks);
            for (i = 0; i < Math.Abs(xLen); i++)
            {
                int rin = ran.Next(0, rMax - 1);
                rChar = tStr.Substring(rin, 1);
                oStr += rChar;
            }
            return oStr;
        }

        /// <summary>
        /// 随机字符串(1-9 A-Y a-y 没有0,o,z )
        /// </summary>
        /// <param name="ran"></param>
        /// <param name="xLen"></param>
        /// <returns></returns>
        public static string RandomNo(Random ran, int xLen)
        {
            string[] char_array = new string[52];
            char_array[0] = "1";
            char_array[1] = "2";
            char_array[2] = "3";
            char_array[3] = "4";
            char_array[4] = "5";
            char_array[5] = "6";
            char_array[6] = "7";
            char_array[7] = "8";
            char_array[8] = "9";
            char_array[9] = "j";
            char_array[10] = "A";
            char_array[11] = "B";
            char_array[12] = "C";
            char_array[13] = "D";
            char_array[14] = "E";
            char_array[15] = "F";
            char_array[16] = "G";
            char_array[17] = "H";
            char_array[18] = "J";
            char_array[19] = "K";
            char_array[20] = "M";
            char_array[21] = "N";
            char_array[22] = "P";
            char_array[23] = "Q";
            char_array[24] = "R";
            char_array[25] = "S";
            char_array[26] = "T";
            char_array[27] = "W";
            char_array[28] = "U";
            char_array[29] = "X";
            char_array[30] = "Y";
            char_array[31] = "a";
            char_array[32] = "b";
            char_array[33] = "c";
            char_array[34] = "d";
            char_array[35] = "e";
            char_array[36] = "f";
            char_array[37] = "g";
            char_array[38] = "h";
            char_array[39] = "i";
            char_array[40] = "k";
            char_array[41] = "m";
            char_array[42] = "n";
            char_array[43] = "p";
            char_array[44] = "q";
            char_array[45] = "r";
            char_array[46] = "s";
            char_array[47] = "t";
            char_array[48] = "w";
            char_array[49] = "u";
            char_array[50] = "x";
            char_array[51] = "y";


            string output = "";
            double tmp = 0;
            while (output.Length < xLen)
            {
                tmp = ran.NextDouble();
                output = output + char_array[(int)(tmp * 52)].ToString();
            }
            return output;
        }

        /// <summary>
        /// 随机数字串(数字 1-9)
        /// </summary>
        /// <param name="ran"></param>
        /// <param name="xLen"></param>
        /// <returns></returns>
        public static string RandomNumNo(Random ran, int xLen)
        {
            string[] char_array = new string[9];
            char_array[0] = "1";
            char_array[1] = "2";
            char_array[2] = "3";
            char_array[3] = "4";
            char_array[4] = "5";
            char_array[5] = "6";
            char_array[6] = "7";
            char_array[7] = "8";
            char_array[8] = "9";

            string output = "";
            double tmp = 0;
            while (output.Length < xLen)
            {
                tmp = ran.NextDouble();
                output = output + char_array[(int)(tmp * 9)].ToString();
            }
            return output;
        }

        /// <summary>
        /// 16位的guid编号(0-9 a-f)
        /// </summary>
        /// <param name="ran"></param>
        /// <returns></returns>
        public static string RandomGuid16(Random ran)
        {
            string[] char_array = new string[16];
            char_array[0] = "1";
            char_array[1] = "2";
            char_array[2] = "3";
            char_array[3] = "4";
            char_array[4] = "5";
            char_array[5] = "6";
            char_array[6] = "7";
            char_array[7] = "8";
            char_array[8] = "9";
            char_array[9] = "0";
            char_array[10] = "a";
            char_array[11] = "b";
            char_array[12] = "c";
            char_array[13] = "d";
            char_array[14] = "e";
            char_array[15] = "f";
            string output = "";
            double tmp = 0;
            while (output.Length < 16)
            {
                tmp = ran.NextDouble();
                output = output + char_array[(int)(tmp * 16)].ToString();
            }
            return output;
        }

        /// <summary>
        /// 随机时间串（带毫秒）
        /// </summary>
        /// <returns></returns>
        public static string RandomTimeNo()
        {
            DateTime date = DateTime.Now;
            string x = date.ToString("yyyyMMddHHmmss");
            string y = date.Millisecond.ToString();
            y = "00" + y;
            y = y.Substring(y.Length - 3, 3);
            return x + y;
        }

        /// <summary>
        /// 产生16位的时间编号号
        /// </summary>
        /// <param name="_ran"></param>
        /// <returns></returns>
        public static string CreateTimeNo(Random _ran)
        {
            return DateTime.Now.ToString("yyyyMMdd") + RndIDNum(_ran, 8);
        }

        /// <summary>
        /// 固定的随机数字
        /// </summary>
        /// <param name="ran"></param>
        /// <param name="xLen"></param>
        /// <returns></returns>
        public static string RndIDNum(Random ran, int xLen)
        {
            string[] char_array = new string[9];
            char_array[0] = "1";
            char_array[1] = "2";
            char_array[2] = "3";
            char_array[3] = "4";
            char_array[4] = "5";
            char_array[5] = "6";
            char_array[6] = "7";
            char_array[7] = "8";
            char_array[8] = "9";

            string output = "";
            double tmp = 0;
            while (output.Length < xLen)
            {
                tmp = ran.NextDouble();
                output = output + char_array[(int)(tmp * 9)].ToString();
            }
            return output;
        }

        /// <summary>
        /// 设置随机数类型的枚举
        /// </summary>
        public enum RandomEnum : int
        {
            /// <summary>
            /// 数字
            /// </summary>
            Numeric = 0,
            /// <summary>
            /// 字母
            /// </summary>
            Letter = 1,
            /// <summary>
            /// 数字字母混合
            /// </summary>
            Blend = 2,
            /// <summary>
            /// 汉字
            /// </summary>
            Chinese = 3
        }
        /// <summary>
        /// 获取全局唯一GUID 值
        /// </summary>
        /// <returns>string</returns>
        private static string RandomChinese() { return RandomChinese(100, true); }
        /// <summary>
        /// 获取当前时间的刻度值
        /// </summary>
        /// <param name="MaxLength">最大长度</param>
        /// <returns>int</returns>
        public static int RandomChinese(int MaxLength) { return SysBase.ToInt(SysBase.Right(DateTime.Now.Ticks.ToString(), 9)); }
        /// <summary>
        /// 获取全局唯一GUID 值
        /// </summary>
        /// <param name="MaxLength">最大长度</param>
        /// <param name="IsRemove">是否去掉特殊字符</param>
        /// <returns>string</returns>
        public static string RandomChinese(int MaxLength, bool IsRemove)
        {
            string Guids = Guid.NewGuid().ToString();
            if (IsRemove) { Guids = Guids.Replace("{", ""); Guids = Guids.Replace("}", ""); Guids = Guids.Replace("-", ""); }
            Guids = SysBase.Left(Guids, MaxLength);
            return Guids;
        }
        /// <summary>
        /// 获取指定类型的随机数
        /// </summary>
        /// <param name="Enum">产生随机数的枚举类型</param>
        /// <param name="MaxLength">最大长度</param>
        /// <returns>string</returns>
        public static string RandomChinese(RandomEnum Enum, int MaxLength)
        {
            return RandomChinese(Enum, "GB2312", MaxLength);
        }

        /// <summary>
        /// 获取随机产生的中文
        /// </summary>
        /// <param name="Encode">产生随机中文的编码</param>
        /// <param name="MaxLength">最大长度</param>
        /// <returns>string</returns>
        public static string RandomChinese(string Encode, int MaxLength)
        {
            //定义返回的字符串
            string Chinese = "";
            //定义中文编码
            Encoding Ecode = Encoding.GetEncoding(Encode);
            Random Rnd = null;// new Random(GetIntRand(4));
            //int Rint = Rnd.Next(1, 100);
            //定义位码、区码的范围数
            int Wint, Qint;
            for (int i = 1; i <= MaxLength; i++)
            {
                int Rint = 0;
                //获取汉字区位
                Rnd = new Random(RandomChinese(9) + i * 1000);
                Rint = Rnd.Next(16, 56);//只获取常用汉字 16-55之间
                Wint = Rint;
                //55区只89个汉字 其他 94个汉字
                Rint = (Wint == 55) ? 90 : 95;
                //定义新种子
                Rnd = new Random(RandomChinese(9) + i * 3000);
                Rint = Rnd.Next(1, Rint);
                Qint = Rint;
                //两个字节变量存储产生的随机汉字区位码
                byte Fbyte = System.Convert.ToByte((Wint + 160).ToString("x"), 16);
                byte Lbyte = System.Convert.ToByte((Qint + 160).ToString("x"), 16);
                //将两个字节变量存储在字节数组中
                byte[] Nbytes = new byte[] { Fbyte, Lbyte };
                Chinese += Ecode.GetString(Nbytes);
                Rnd = null;
            }
            Ecode = null;
            return Chinese;
        }

        /// <summary>
        /// 获取指定类型的随机数
        /// </summary>
        /// <param name="Enum">产生随机数的枚举类型</param>
        /// <param name="Encode">产生随机中文的编码</param>
        /// <param name="MaxLength">最大长度</param>
        /// <returns>string</returns>
        public static string RandomChinese(RandomEnum Enum, string Encode, int MaxLength)
        {
            if (Enum == RandomEnum.Chinese) return RandomChinese(Encode, MaxLength);
            string Guids = "";
            for (int i = 1; i <= MaxLength; i++)
            {
                int MinInt, MaxInt, Num;
                Random Rand = new Random(RandomChinese(9) + i * 1000);
                Num = (Enum == RandomEnum.Blend) ? ((Rand.Next(0, 10) <= 4) ? (int)RandomEnum.Numeric : (int)RandomEnum.Letter) : (int)Enum;
                MinInt = Num == 0 ? 48 : 97;
                MaxInt = Num == 0 ? 57 : 122;
                Guids += SysBase.IntToString(Rand.Next(MinInt, MaxInt));
                Rand = null;
            }
            return Guids;
        }

        #endregion

        #region 字符串处理

        /// <summary>
        /// 关键字上色处理
        /// </summary>
        /// <param name="Object">要操作的 string  数据</param>
        /// <param name="key">关键字</param>
        /// <param name="css">样式名称</param>
        /// <returns>string</returns>
        public static string AddColor(string Object, string key, string css)
        {
            StringBuilder Builders = new StringBuilder(Object);
            Builders.Replace(key, "<span class=\"" + css + "\">" + key + "</span>");
            return Builders.ToString();
        }

        /// <summary>
        /// 字符串截取函数
        /// [len是字节，解决了汉字与英文字节不一样导致直接截取到的长度不一样的问题]
        /// </summary>
        /// <param name="inputString"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public static string CutString(string s, int len)
        {
            string result = ""; //最终返回的结果
            int byteLen = System.Text.Encoding.Default.GetByteCount(s);  //单字节字符长度
            int charLen = s.Length; //把字符平等对待时的字符串长度
            int byteCount = 0;  //记录读取进度{中文按两单位计算}
            int pos = 0;    //记录截取位置{中文按两单位计算}
            if (byteLen > len)
            {
                for (int i = 0; i < charLen; i++)
                {
                    if (Convert.ToInt32(s.ToCharArray()[i]) > 255)  //遇中文字符计数加2
                        byteCount += 2;
                    else         //按英文字符计算加1
                        byteCount += 1;
                    if (byteCount > len)   //到达指定长度时，记录指针位置并停止
                    {
                        pos = i;
                        break;
                    }
                }
                result = s.Substring(0, pos);
            }
            else
                result = s;
            return result;
        }

        /// <summary>
        /// 字符串截取函数[带省略号]
        /// [len是字节，解决了汉字与英文字节不一样导致直接截取到的长度不一样的问题]
        /// </summary>
        /// <param name="inputString"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public static string CutStringDot(string s, int len)
        {
            string result = ""; //最终返回的结果
            int byteLen = System.Text.Encoding.Default.GetByteCount(s);  //单字节字符长度
            int charLen = s.Length; //把字符平等对待时的字符串长度
            int byteCount = 0;  //记录读取进度{中文按两单位计算}
            int pos = 0;    //记录截取位置{中文按两单位计算}
            if (byteLen > len)
            {
                for (int i = 0; i < charLen; i++)
                {
                    if (Convert.ToInt32(s.ToCharArray()[i]) > 255)  //遇中文字符计数加2
                        byteCount += 2;
                    else         //按英文字符计算加1
                        byteCount += 1;
                    if (byteCount >= len)   //到达指定长度时，记录指针位置并停止
                    {
                        pos = i;
                        break;
                    }
                }
                result = s.Substring(0, pos) + "...";
            }
            else
                result = s;

            return result;
        }




        /// <summary>
        /// 过滤文本中的所有HTML标签
        /// </summary>
        /// <param name="html">要过滤的文本</param>
        /// <returns>返回无html标签文本</returns>
        public static string RemoveHTML(string html)
        {
            return System.Text.RegularExpressions.Regex.Replace(html, "<(.[^>]*)>", "");
        }

        /// <summary>
        /// 过滤换行和空格
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static string FilterNewLineBlank(string html)
        {
            html = Regex.Replace(html, "[\f\n\r\t\v]", "");
            html = Regex.Replace(html, " {2,}", " ");
            html = Regex.Replace(html, ">[ ]{1}", ">");
            return html;
        }

        /// <summary>
        /// 文本操作 执行HTML解码
        /// </summary>
        /// <param name="Object">要转换的对象</param>
        /// <returns>字符</returns>
        public static string HtmlDecode(string Object)
        {
            if (SysBase.IsNull(Object)) return "";
            StringBuilder Builder = new StringBuilder(Object);
            Builder.Replace("&amp;", "&");
            Builder.Replace("&lt;", "<");
            Builder.Replace("&gt;", ">");
            Builder.Replace("&quot;", "\"");
            Builder.Replace("&#39;", "'");
            Builder.Replace("&nbsp; &nbsp; ", "\t");
            Builder.Replace("<p></p>", "\r\n\r\n");
            Builder.Replace("<br />", "\r\n");
            Builder.Replace("<br>", "\r\n");
            return Builder.ToString();
        }
        /// <summary>
        /// 文本操作 执行HTML编码
        /// </summary>
        /// <param name="Object">要转换的对象</param>
        /// <returns>字符</returns>
        public static string HtmlEncode(string Object)
        {
            if (SysBase.IsNull(Object)) return "";
            StringBuilder Builder = new StringBuilder(Object);
            Builder.Replace("&", "&amp;");
            Builder.Replace("<", "&lt;");
            Builder.Replace(">", "&gt;");
            Builder.Replace("\"", "&quot;");
            Builder.Replace("'", "&#39;");
            Builder.Replace("\t", "&nbsp; &nbsp; ");
            Builder.Replace("\r\n\r\n", "<p></p>");
            Builder.Replace("\r\n", "<br />");
            return Builder.ToString();
        }

        /// <summary>
        /// 获取一定范围内的数据如[A][A]
        /// </summary>
        /// <param name="text"></param>
        /// <param name="reg"></param>
        /// <returns></returns>
        public static string GetHtmlScopeCont(string text, string reg)
        {
            Regex re = new Regex(@"\" + reg + @"(.+?)\" + reg, RegexOptions.IgnoreCase);
            MatchCollection matches = re.Matches(text);
            foreach (Match m in matches)
            {
                if (m.Groups[1].Value.Length > 0) return m.Groups[1].Value;
            }
            return "";
        }

        /// <summary>
        /// 过滤获取网页最外层嵌套 如[<table></table>]
        /// </summary>
        /// <param name="text">源字符内容</param>
        /// <param name="tagName">嵌套标签，如table</param>
        /// <returns></returns>
        public static List<string> GetHtmlTagCont(string text, string tagName)
        {
            List<string> list = new List<string>();
            Regex re = new Regex(string.Format(@"(?isx)<{0}[^>]*>(?><{0}[^>]*>(?<Open>)|</{0}>(?<-Open>)|(?:(?!</?{0}\b).)*)*(?(Open)(?!))</{0}>"
                , tagName)
                , RegexOptions.IgnoreCase);
            MatchCollection matches = re.Matches(text);
            foreach (Match m in matches)
            {
                if (m.Groups[0].Value.Length > 0) list.Add(m.Groups[0].Value);
            }
            return list;
        }


        /// <summary>
        /// 获取单个图片路径
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static string GetImgUrl(string html)
        {//(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<url>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", 
            string str = string.Empty;//="(?<=src=\").+?\\.(gif|jpg|bmp|png)(?=\")"
            Regex r = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<url>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.Compiled);//<img\s+[^>]*\s*src\s*=\s*([']?)(?<url>\S+)'?[^>]*>
            Match m = r.Match(html.ToLower());
            if (m.Success)
                str = m.Result("${url}");
            str = str.Replace("\"", string.Empty);
            str = str.Replace("\'", string.Empty);
            return str;
        }

        /// <summary>
        /// 获取图片路径
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static string GetImgUrlList(string html)
        {
            string str = string.Empty;
            string pat = @"<img\s+[^>]*\s*src\s*=\s*([']?)(?<url>\S+)'?[^>]*>";
            Regex r = new Regex(pat, RegexOptions.Compiled);
            Match m = r.Match(html.ToLower());
            while (m.Success)
            {
                Group g = m.Groups[2];
                //str.Append(g).Append(",");
                str += g.Value;
                str += ",";
                m = m.NextMatch();
            }
            if (str.LastIndexOf(",") > 0)
                str = str.Substring(0, str.Length - 1);
            str = str.Replace("\"", string.Empty);
            str = str.Replace("\'", string.Empty);
            return str;
        }

        /// <summary>
        /// 获取视频源
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static string GetFilterVideo(string html)
        {
            string str = string.Empty;
            Regex r = new Regex(@"(?i)<embed[^>]*?src=(['""]?)(?<src>[^'""\s>]+)\1[^>]*>", RegexOptions.Compiled);
            MatchCollection mc = r.Matches(html.ToLower());
            foreach (Match m in mc)
            {
                if (m.Success)
                    str = m.Groups["src"].Value;
                if (!string.IsNullOrEmpty(str))
                    break;
            }
            str = str.Replace("\"", string.Empty);
            str = str.Replace("\'", string.Empty);
            return str;
        }

        #endregion

        #region 中文转拼音
        //汉字内码数组
        private static int[] pyValue = new int[]
        {
        -20319,-20317,-20304,-20295,-20292,-20283,-20265,-20257,-20242,-20230,-20051,-20036,
        -20032,-20026,-20002,-19990,-19986,-19982,-19976,-19805,-19784,-19775,-19774,-19763,
        -19756,-19751,-19746,-19741,-19739,-19728,-19725,-19715,-19540,-19531,-19525,-19515,
        -19500,-19484,-19479,-19467,-19289,-19288,-19281,-19275,-19270,-19263,-19261,-19249,
        -19243,-19242,-19238,-19235,-19227,-19224,-19218,-19212,-19038,-19023,-19018,-19006,
        -19003,-18996,-18977,-18961,-18952,-18783,-18774,-18773,-18763,-18756,-18741,-18735,
        -18731,-18722,-18710,-18697,-18696,-18526,-18518,-18501,-18490,-18478,-18463,-18448,
        -18447,-18446,-18239,-18237,-18231,-18220,-18211,-18201,-18184,-18183, -18181,-18012,
        -17997,-17988,-17970,-17964,-17961,-17950,-17947,-17931,-17928,-17922,-17759,-17752,
        -17733,-17730,-17721,-17703,-17701,-17697,-17692,-17683,-17676,-17496,-17487,-17482,
        -17468,-17454,-17433,-17427,-17417,-17202,-17185,-16983,-16970,-16942,-16915,-16733,
        -16708,-16706,-16689,-16664,-16657,-16647,-16474,-16470,-16465,-16459,-16452,-16448,
        -16433,-16429,-16427,-16423,-16419,-16412,-16407,-16403,-16401,-16393,-16220,-16216,
        -16212,-16205,-16202,-16187,-16180,-16171,-16169,-16158,-16155,-15959,-15958,-15944,
        -15933,-15920,-15915,-15903,-15889,-15878,-15707,-15701,-15681,-15667,-15661,-15659,
        -15652,-15640,-15631,-15625,-15454,-15448,-15436,-15435,-15419,-15416,-15408,-15394,
        -15385,-15377,-15375,-15369,-15363,-15362,-15183,-15180,-15165,-15158,-15153,-15150,
        -15149,-15144,-15143,-15141,-15140,-15139,-15128,-15121,-15119,-15117,-15110,-15109,
        -14941,-14937,-14933,-14930,-14929,-14928,-14926,-14922,-14921,-14914,-14908,-14902,
        -14894,-14889,-14882,-14873,-14871,-14857,-14678,-14674,-14670,-14668,-14663,-14654,
        -14645,-14630,-14594,-14429,-14407,-14399,-14384,-14379,-14368,-14355,-14353,-14345,
        -14170,-14159,-14151,-14149,-14145,-14140,-14137,-14135,-14125,-14123,-14122,-14112,
        -14109,-14099,-14097,-14094,-14092,-14090,-14087,-14083,-13917,-13914,-13910,-13907,
        -13906,-13905,-13896,-13894,-13878,-13870,-13859,-13847,-13831,-13658,-13611,-13601,
        -13406,-13404,-13400,-13398,-13395,-13391,-13387,-13383,-13367,-13359,-13356,-13343,
        -13340,-13329,-13326,-13318,-13147,-13138,-13120,-13107,-13096,-13095,-13091,-13076,
        -13068,-13063,-13060,-12888,-12875,-12871,-12860,-12858,-12852,-12849,-12838,-12831,
        -12829,-12812,-12802,-12607,-12597,-12594,-12585,-12556,-12359,-12346,-12320,-12300,
        -12120,-12099,-12089,-12074,-12067,-12058,-12039,-11867,-11861,-11847,-11831,-11798,
        -11781,-11604,-11589,-11536,-11358,-11340,-11339,-11324,-11303,-11097,-11077,-11067,
        -11055,-11052,-11045,-11041,-11038,-11024,-11020,-11019,-11018,-11014,-10838,-10832,
        -10815,-10800,-10790,-10780,-10764,-10587,-10544,-10533,-10519,-10331,-10329,-10328,
        -10322,-10315,-10309,-10307,-10296,-10281,-10274,-10270,-10262,-10260,-10256,-10254
        };
        //汉字拼音数组
        private static string[] pyName = new string[]
        {
        "a","ai","an","ang","ao","ba","bai","ban","bang","bao","bei","ben","beng","bi",
        "bian","biao","bie","bin","bing","bo","bu","ca","cai","can","cang","cao","ce",
        "ceng","cha","chai","chan","chang","chao","che","chen","cheng","chi","chong",
        "chou","chu","chuai","chuan","chuang","chui","chun","chuo","ci","cong","cou",
        "cu","cuan","cui","cun","cuo","da","dai","dan","dang","dao","de","deng","di",
        "dian","diao","die","ding","diu","dong","dou","du","duan","dui","dun","duo","e",
        "en","er","fa","fan","fang","fei","fen","feng","fo","fou","fu","ga","gai","gan",
        "gang","gao","ge","gei","gen","geng","gong","gou","gu","gua","guai","guan",
        "guang","gui","gun","guo","ha","hai","han","hang","hao","he","hei","hen","heng",
        "hong","hou","hu","hua","huai","huan","huang","hui","hun","huo","ji","jia","jian",
        "jiang","jiao","jie","jin","jing","jiong","jiu","ju","juan","jue","jun","ka","kai",
        "kan","kang","kao","ke","ken", "keng","kong","kou","ku","kua","kuai","kuan","kuang",
        "kui","kun","kuo","la","lai","lan","lang","lao","le","lei","leng","li","lia","lian",
        "liang","liao","lie","lin","ling","liu","long","lou","lu","lv","luan","lue","lun",
        "luo","ma","mai","man","mang","mao","me","mei","men","meng","mi","mian","miao",
        "mie","min","ming","miu","mo","mou","mu","na","nai","nan","nang","nao","ne",
        "nei","nen","neng","ni","nian","niang","niao","nie","nin","ning","niu","nong",
        "nu","nv","nuan","nue","nuo","o","ou","pa","pai","pan","pang","pao","pei","pen",
        "peng","pi","pian","piao","pie","pin","ping","po","pu","qi","qia","qian","qiang",
        "qiao","qie","qin","qing","qiong","qiu","qu","quan","que","qun","ran","rang",
        "rao","re","ren","reng","ri","rong","rou","ru","ruan","rui","run","ruo","sa",
        "sai","san","sang","sao","se","sen","seng","sha","shai","shan","shang","shao",
        "she","shen","sheng","shi","shou","shu","shua","shuai","shuan","shuang","shui",
        "shun","shuo","si","song","sou","su","suan","sui","sun","suo","ta","tai","tan",
        "tang","tao","te","teng","ti","tian","tiao","tie","ting","tong","tou","tu","tuan",
        "tui","tun","tuo","wa","wai","wan","wang","wei","wen","weng","wo","wu","xi",
        "xia","xian","xiang","xiao","xie","xin","xing","xiong","xiu","xu","xuan","xue",
        "xun","ya","yan","yang","yao","ye","yi","yin","ying","yo","yong","you","yu",
        "yuan","yue","yun","za","zai","zan","zang","zao","ze","zei","zen","zeng","zha",
        "zhai","zhan","zhang","zhao","zhe","zhen","zheng","zhi","zhong","zhou","zhu",
        "zhua","zhuai","zhuan","zhuang","zhui","zhun","zhuo","zi","zong","zou","zu",
        "zuan","zui","zun","zuo"
        };
        /// <summary>
        /// 把汉字转换成拼音(全拼)
        /// </summary>
        /// <param name="text">汉字字符串</param>
        /// <returns>转换后的拼音(全拼)字符串</returns>
        public static string ChineseToPy(string text)
        {
            // 匹配中文字符
            Regex regex = new Regex("^[\u4e00-\u9fa5]$");
            byte[] array = new byte[2];
            string pyString = "";
            int chrAsc = 0;
            int i1 = 0;
            int i2 = 0;
            char[] noWChar = text.ToCharArray();

            for (int j = 0; j < noWChar.Length; j++)
            {
                // 中文字符
                if (regex.IsMatch(noWChar[j].ToString()))
                {
                    array = System.Text.Encoding.Default.GetBytes(noWChar[j].ToString());
                    i1 = (short)(array[0]);
                    i2 = (short)(array[1]);
                    chrAsc = i1 * 256 + i2 - 65536;
                    if (chrAsc > 0 && chrAsc < 160)
                    {
                        pyString += noWChar[j];
                    }
                    else
                    {
                        // 修正部分文字
                        if (chrAsc == -9254)  // 修正“圳”字
                            pyString += "Zhen";
                        else
                        {
                            for (int i = (pyValue.Length - 1); i >= 0; i--)
                            {
                                if (pyValue[i] <= chrAsc)
                                {
                                    pyString += pyName[i];
                                    break;
                                }
                            }
                        }
                    }
                }
                // 非中文字符
                else
                {
                    pyString += noWChar[j].ToString().ToUpper();
                }
            }
            return pyString;
        }
        #endregion 中文转换拼音

        #region 中文转数字 16进制
        public static string StrToNum16(string character)
        {
            string coding = "";
            for (int i = 0; i < character.Length; i++)
            {
                byte[] bytes = System.Text.Encoding.Unicode.GetBytes(character.Substring(i, 1)); //取出二进制编码内容
                string lowCode = System.Convert.ToString(bytes[0], 16); //取出低字节编码内容（两位16进制）
                if (lowCode.Length == 1)
                    lowCode = "0" + lowCode;
                string hightCode = System.Convert.ToString(bytes[1], 16);//取出高字节编码内容（两位16进制）
                if (hightCode.Length == 1)
                    hightCode = "0" + hightCode;
                coding += (lowCode + hightCode);//加入到字符串中,
            }
            return coding;
        }
        public static string NumToStr16(string coding)
        {
            string characters = "";
            if (coding.Length % 4 != 0)//编码为16进制,必须为4的倍数。
            {
                throw new System.Exception("编码格式不正确");
            }
            for (int i = 0; i < coding.Length; i += 4) //每四位为一个汉字
            {
                byte[] bytes = new byte[2];
                string lowCode = coding.Substring(i, 2); //取出低字节,并以16进制进制转换
                bytes[0] = System.Convert.ToByte(lowCode, 16);
                string highCode = coding.Substring(i + 2, 2); //取出高字节,并以16进制进行转换
                bytes[1] = System.Convert.ToByte(highCode, 16);
                string character = System.Text.Encoding.Unicode.GetString(bytes);
                characters += character;
            }
            return characters;
        }
        #endregion

        #region 中文转数字 10进制
        public static string StrToNum(string character)
        {
            string coding = "";
            for (int i = 0; i < character.Length; i++)
            {
                byte[] bytes = System.Text.Encoding.Unicode.GetBytes(character.Substring(i, 1)); //取出二进制编码内容
                string lowCode = System.Convert.ToString(bytes[0], 10).PadLeft(3, '0');//取出低字节编码内容（两位16进制）
                string hightCode = System.Convert.ToString(bytes[1], 10).PadLeft(3, '0');//取出高字节编码内容（两位16进制）
                coding += (lowCode + hightCode);//加入到字符串中,
            }
            return coding;
        }
        public static string NumToStr(string coding)
        {
            string characters = "";
            if (coding.Length % 6 != 0)//编码为16进制,必须为4的倍数。
            {
                throw new System.Exception("编码格式不正确");
            }
            for (int i = 0; i < coding.Length; i += 6) //每四位为一个汉字
            {
                byte[] bytes = new byte[2];
                string lowCode = coding.Substring(i, 3); //取出低字节,并以16进制进制转换
                bytes[0] = System.Convert.ToByte(lowCode, 10);
                string highCode = coding.Substring(i + 3, 3); //取出高字节,并以16进制进行转换
                bytes[1] = System.Convert.ToByte(highCode, 10);
                string character = System.Text.Encoding.Unicode.GetString(bytes);
                characters += character;
            }
            return characters;
        }
        #endregion

        #region ASCII码和字符互转
        //字符转数字
        public static int CharToAsc(string character)
        {
            if (character.Length == 1)
            {
                System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
                int intAsciiCode = (int)asciiEncoding.GetBytes(character)[0];
                return (intAsciiCode);
            }
            else
            {
                throw new Exception("字符串不可用。");
            }

        }

        //ASCII码转字符
        public static string AscToChar(int asciiCode)
        {
            if (asciiCode >= 0 && asciiCode <= 255)
            {
                System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
                byte[] byteArray = new byte[] { (byte)asciiCode };
                string strCharacter = asciiEncoding.GetString(byteArray);
                return (strCharacter);
            }
            else
            {
                throw new Exception("ASCII码不可用。");
            }
        }
        #endregion

    }


    /// <summary>
    /// 将汉字转换成为拼音
    /// </summary>
    public class XXchs2py
    {
        private static int[] pyvalue = new int[]{-20319,-20317,-20304,-20295,-20292,-20283,-20265,-20257,-20242,-20230,-20051,-20036,-20032,-20026,
    -20002,-19990,-19986,-19982,-19976,-19805,-19784,-19775,-19774,-19763,-19756,-19751,-19746,-19741,-19739,-19728,
    -19725,-19715,-19540,-19531,-19525,-19515,-19500,-19484,-19479,-19467,-19289,-19288,-19281,-19275,-19270,-19263,
    -19261,-19249,-19243,-19242,-19238,-19235,-19227,-19224,-19218,-19212,-19038,-19023,-19018,-19006,-19003,-18996,
    -18977,-18961,-18952,-18783,-18774,-18773,-18763,-18756,-18741,-18735,-18731,-18722,-18710,-18697,-18696,-18526,
    -18518,-18501,-18490,-18478,-18463,-18448,-18447,-18446,-18239,-18237,-18231,-18220,-18211,-18201,-18184,-18183,
    -18181,-18012,-17997,-17988,-17970,-17964,-17961,-17950,-17947,-17931,-17928,-17922,-17759,-17752,-17733,-17730,
    -17721,-17703,-17701,-17697,-17692,-17683,-17676,-17496,-17487,-17482,-17468,-17454,-17433,-17427,-17417,-17202,
    -17185,-16983,-16970,-16942,-16915,-16733,-16708,-16706,-16689,-16664,-16657,-16647,-16474,-16470,-16465,-16459,
    -16452,-16448,-16433,-16429,-16427,-16423,-16419,-16412,-16407,-16403,-16401,-16393,-16220,-16216,-16212,-16205,
    -16202,-16187,-16180,-16171,-16169,-16158,-16155,-15959,-15958,-15944,-15933,-15920,-15915,-15903,-15889,-15878,
    -15707,-15701,-15681,-15667,-15661,-15659,-15652,-15640,-15631,-15625,-15454,-15448,-15436,-15435,-15419,-15416,
    -15408,-15394,-15385,-15377,-15375,-15369,-15363,-15362,-15183,-15180,-15165,-15158,-15153,-15150,-15149,-15144,
    -15143,-15141,-15140,-15139,-15128,-15121,-15119,-15117,-15110,-15109,-14941,-14937,-14933,-14930,-14929,-14928,
    -14926,-14922,-14921,-14914,-14908,-14902,-14894,-14889,-14882,-14873,-14871,-14857,-14678,-14674,-14670,-14668,
    -14663,-14654,-14645,-14630,-14594,-14429,-14407,-14399,-14384,-14379,-14368,-14355,-14353,-14345,-14170,-14159,
    -14151,-14149,-14145,-14140,-14137,-14135,-14125,-14123,-14122,-14112,-14109,-14099,-14097,-14094,-14092,-14090,
    -14087,-14083,-13917,-13914,-13910,-13907,-13906,-13905,-13896,-13894,-13878,-13870,-13859,-13847,-13831,-13658,
    -13611,-13601,-13406,-13404,-13400,-13398,-13395,-13391,-13387,-13383,-13367,-13359,-13356,-13343,-13340,-13329,
    -13326,-13318,-13147,-13138,-13120,-13107,-13096,-13095,-13091,-13076,-13068,-13063,-13060,-12888,-12875,-12871,
    -12860,-12858,-12852,-12849,-12838,-12831,-12829,-12812,-12802,-12607,-12597,-12594,-12585,-12556,-12359,-12346,
    -12320,-12300,-12120,-12099,-12089,-12074,-12067,-12058,-12039,-11867,-11861,-11847,-11831,-11798,-11781,-11604,
    -11589,-11536,-11358,-11340,-11339,-11324,-11303,-11097,-11077,-11067,-11055,-11052,-11045,-11041,-11038,-11024,
    -11020,-11019,-11018,-11014,-10838,-10832,-10815,-10800,-10790,-10780,-10764,-10587,-10544,-10533,-10519,-10331,
    -10329,-10328,-10322,-10315,-10309,-10307,-10296,-10281,-10274,-10270,-10262,-10260,-10256,-10254};
        private static string[] pystr = new string[]{"a","ai","an","ang","ao","ba","bai","ban","bang","bao","bei","ben","beng","bi","bian","biao",
   "bie","bin","bing","bo","bu","ca","cai","can","cang","cao","ce","ceng","cha","chai","chan","chang","chao","che","chen",
   "cheng","chi","chong","chou","chu","chuai","chuan","chuang","chui","chun","chuo","ci","cong","cou","cu","cuan","cui",
   "cun","cuo","da","dai","dan","dang","dao","de","deng","di","dian","diao","die","ding","diu","dong","dou","du","duan",
   "dui","dun","duo","e","en","er","fa","fan","fang","fei","fen","feng","fo","fou","fu","ga","gai","gan","gang","gao",
   "ge","gei","gen","geng","gong","gou","gu","gua","guai","guan","guang","gui","gun","guo","ha","hai","han","hang",
   "hao","he","hei","hen","heng","hong","hou","hu","hua","huai","huan","huang","hui","hun","huo","ji","jia","jian",
   "jiang","jiao","jie","jin","jing","jiong","jiu","ju","juan","jue","jun","ka","kai","kan","kang","kao","ke","ken",
   "keng","kong","kou","ku","kua","kuai","kuan","kuang","kui","kun","kuo","la","lai","lan","lang","lao","le","lei",
   "leng","li","lia","lian","liang","liao","lie","lin","ling","liu","long","lou","lu","lv","luan","lue","lun","luo",
   "ma","mai","man","mang","mao","me","mei","men","meng","mi","mian","miao","mie","min","ming","miu","mo","mou","mu",
   "na","nai","nan","nang","nao","ne","nei","nen","neng","ni","nian","niang","niao","nie","nin","ning","niu","nong",
   "nu","nv","nuan","nue","nuo","o","ou","pa","pai","pan","pang","pao","pei","pen","peng","pi","pian","piao","pie",
   "pin","ping","po","pu","qi","qia","qian","qiang","qiao","qie","qin","qing","qiong","qiu","qu","quan","que","qun",
   "ran","rang","rao","re","ren","reng","ri","rong","rou","ru","ruan","rui","run","ruo","sa","sai","san","sang",
   "sao","se","sen","seng","sha","shai","shan","shang","shao","she","shen","sheng","shi","shou","shu","shua",
   "shuai","shuan","shuang","shui","shun","shuo","si","song","sou","su","suan","sui","sun","suo","ta","tai",
   "tan","tang","tao","te","teng","ti","tian","tiao","tie","ting","tong","tou","tu","tuan","tui","tun","tuo",
   "wa","wai","wan","wang","wei","wen","weng","wo","wu","xi","xia","xian","xiang","xiao","xie","xin","xing",
   "xiong","xiu","xu","xuan","xue","xun","ya","yan","yang","yao","ye","yi","yin","ying","yo","yong","you",
   "yu","yuan","yue","yun","za","zai","zan","zang","zao","ze","zei","zen","zeng","zha","zhai","zhan","zhang",
   "zhao","zhe","zhen","zheng","zhi","zhong","zhou","zhu","zhua","zhuai","zhuan","zhuang","zhui","zhun","zhuo",
   "zi","zong","zou","zu","zuan","zui","zun","zuo"};
        public XXchs2py()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        public static string convert(string chrstr)
        {
            byte[] array = new byte[2];
            string returnstr = "";
            int chrasc = 0;
            int i1 = 0;
            int i2 = 0;
            char[] nowchar = chrstr.ToCharArray();
            for (int j = 0; j < nowchar.Length; j++)
            {
                array = System.Text.Encoding.Default.GetBytes(nowchar[j].ToString());
                i1 = (short)(array[0]);
                i2 = (short)(array[1]);

                chrasc = i1 * 256 + i2 - 65536;
                if (chrasc > 0 && chrasc < 160)
                {
                    returnstr += nowchar[j];
                }
                else
                {
                    for (int i = (pyvalue.Length - 1); i >= 0; i--)
                    {
                        if (pyvalue[i] <= chrasc)
                        {
                            returnstr += pystr[i];
                            break;
                        }
                    }
                }
            }
            return returnstr;
        }
    }
}
